package com.qiwx.pigeon.base;

import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.facebook.common.internal.Throwables;
import com.qiwx.pigeon.Utils.LogUtil;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.eventbus.DbUpdateEvent;
import com.qiwx.pigeon.helper.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * @description
 * @Author qiwx
 * @Date 2020-03-23 13:55
 **/
public abstract class BaseFragment extends Fragment implements IBasePolicy {
    private Unbinder mUnbinder;
    public View view;
    protected String TAG = getClass().getSimpleName();
    private int mShowNetCount = 0;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        mUnbinder = ButterKnife.bind(this, view);
        initAfterOnCreate();
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);

        }
        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onGetStickyEvent(DbUpdateEvent updateEvent) {
        LogUtil.e("TAG", "数据库更新");
        dbUpdate();

    }


    public void showError(String error) {
        dismissLoading();
        MyToast.showLong(error);
    }

    public void showLoading() {
        mustRunOnMainThread();
        mShowNetCount++;
        LogUtil.e(TAG, "show mShowNetCount:" + mShowNetCount);
        LoadingDialog.showLoading(getActivity());
    }

    public void showLoading(String msg) {
        mustRunOnMainThread();
        mShowNetCount++;
        LogUtil.e(TAG, "show 1 mShowNetCount:" + mShowNetCount);
        LoadingDialog.showLoading(getActivity(), msg, true);
    }

    public void showLoading(String msg, boolean isCanCancel) {
        mustRunOnMainThread();
        mShowNetCount++;
        LogUtil.e(TAG, "show 2 mShowNetCount:" + mShowNetCount);
        LoadingDialog.showLoading(getActivity(), msg, isCanCancel);
    }

    public void dismissLoading() {
        mustRunOnMainThread();
        mShowNetCount--;
        if (mShowNetCount <= 0) {
            mShowNetCount = 0;
            LoadingDialog.disDialog();
        }
        LogUtil.e(TAG, "dismiss mShowNetCount:" + mShowNetCount);
    }

    public void onSingleLoginView(String msg) {

    }//单点登录  被踢 接口

    public void onNoLogin(String msg) {

    }//未登录

    public void onUserDisable(String msg) {

    }//禁止用户

    public void dbUpdate() {

    }
    private void mustRunOnMainThread() {
        if (Looper.getMainLooper() != Looper.myLooper()) {
            Throwables.propagate(new IllegalThreadStateException("方法必须在主线程调用"));
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null) {
            mUnbinder.unbind();
            mUnbinder = null;
        }
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }

    }
}
